﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TfsBacklogMgr.Main
{
    public class TestFactory : IFactory
    {
        public Context LoadSettings(string filepath)
        {
            LoadUsers();
            LoadIterations();
            LoadWorkItems();
            Context ctx = Context.GetContext();
            ctx.Filename = filepath;
            Context.Load();
            return ctx;
        }

        public void SaveSettings(Context ctx)
        {
            // do nothing
        }

        public void ConnectToTfs()
        {
            // do nothing
        }


#region Implementation details - dummy data

        public void LoadUsers()
        {
            if (User.GetUsers().Count == 0)
            {
                User.Add(new User() { Id = 11, Login = "christian", Name = "Christian Mogensen" });
                User.Add(new User() { Id = 22, Login = "jens", Name = "Jens M. Glattetre" });
                User.Add(new User() { Id = 33, Login = "marek", Name = "Marek Vokac" });
                User.Add(new User() { Id = 44, Login = "helene", Name = "Helene F Hansen" });
                User.Add(new User() { Id = 55, Login = "janp", Name = "Jan Petter Hagberg" });
            }
        }

        public void LoadIterations()
        {
            if (Iteration.GetAllIterations().Count == 0)
            {
                Iteration.GetIteration("$");
                Iteration.GetIteration("$/7.1/Stake/Sprint 1");
                Iteration.GetIteration("$/7.1/Stake/Sprint 2");
                Iteration.GetIteration("$/7.1/Find/Sprint 1");
                Iteration stake = Iteration.GetIteration("$/7.1/Stake");
                stake.IsBacklogIteration = true;
                Iteration stakeback = Iteration.GetIteration("$/7.1/Stake/Backlog");
                stakeback.IsBacklogIteration = true;
                Iteration find = Iteration.GetIteration("$/7.1/Find");
                find.IsBacklogIteration = true;
            }
        }

        public void LoadWorkItems()
        {
            LoadIterations();
            if (WorkItem.GetWorkItem(11) == null)
            {
                WorkItem us1 = new WorkItem( new MockWorkItem("UserStory", User.GetUser(11), 8,0,0, null) { Id = 11, Title = "Sale Archive", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Sprint 1" } );
                WorkItem us2 = new WorkItem( new MockWorkItem("UserStory", User.GetUser(11), 12, 0,0, null) { Id = 12, Title = "Stakeholder Add Dialog", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Sprint 1" } );
                WorkItem us3 = new WorkItem( new MockWorkItem("UserStory", User.GetUser(11), 10, 0,0, null)  { Id = 13, Title = "Stakeholder Edit Dialog", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Backlog" } );
                WorkItem us4 = new WorkItem( new MockWorkItem("UserStory", User.GetUser(11), 6, 0,0, null)  { Id = 14, Title = "Stakeholder Checkbox", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Backlog" } );
                WorkItem us5 = new WorkItem( new MockWorkItem("UserStory", User.GetUser(11), 6, 0,0, null)  { Id = 15, Title = "Find dialog", AreaPath = "$/Find", IterationPath = "$/7.1/Find" } );

                WorkItem.Add(us1);
                WorkItem.Add(new WorkItem( new MockWorkItem("Task",User.GetUser(22),0,6,6, us1) { Id = 111, Title = "Bug me later", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Sprint 1" }));
                WorkItem.Add(new WorkItem( new MockWorkItem("Task",User.GetUser(33),0,6,6, us1) { Id = 122, Title = "Make archive provider", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Sprint 1"}));
                WorkItem.Add(new WorkItem( new MockWorkItem("Task",User.GetUser(44),0,8,6, us1) { Id = 113, Title = "Make archive view", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake"}));
                WorkItem.Add(new WorkItem( new MockWorkItem("Task",User.GetUser(22),0,12,11, us1) { Id = 114, Title = "Sentry fixes", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Sprint 1"}));
                WorkItem.Add(new WorkItem( new MockWorkItem("Task",User.GetUser(33),0,6,9, us1) { Id = 155, Title = "Checkbox for archive", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Sprint 2"}));

                WorkItem.Add(new WorkItem( new MockWorkItem("Bug",User.GetUser(22),0,12,9, null) { Id = 117, Title = "Sentry fixes", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Sprint 1" }));

                WorkItem.Add(us2);
                WorkItem.Add(new WorkItem(new MockWorkItem("Task",User.GetUser(44),0,6,2, us2)  { Id = 211, Title = "Make stakeholder dialog", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Sprint 2" }));
                WorkItem.Add(new WorkItem(new MockWorkItem("Task",User.GetUser(22),0,6,3, us2)  { Id = 222, Title = "Make stakeholder data model web", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake" }));
                WorkItem.Add(new WorkItem(new MockWorkItem("Task",User.GetUser(33),0,7,3, us2)  { Id = 213, Title = "Make stakeholder data model win",  AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Sprint 1"}));
                WorkItem.Add(new WorkItem(new MockWorkItem("Task",User.GetUser(33),0,7,2, us2)  { Id = 214, Title = "Make stakeholder sentry win", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Sprint 1"}));
                WorkItem.Add(new WorkItem(new MockWorkItem("Task",User.GetUser(22),0,7,1, us2)  { Id = 216, Title = "Make stakeholder sentry web", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake"}));

                WorkItem.Add(new WorkItem(new MockWorkItem("Bug", User.GetUser(22), 0, 8, 8, null) { Id = 217, Title = "Fix something", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake" }));

                WorkItem.Add(us3);
                WorkItem.Add(new WorkItem( new MockWorkItem("Task",User.GetUser(33),0,8,8, us3)  { Id = 311, Title = "Make stakeholder edit dialog win", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Backlog" }));
                WorkItem.Add(new WorkItem( new MockWorkItem("Task",User.GetUser(22),0,8,8, us3)  { Id = 312, Title = "Make stakeholder edit dialog web", AreaPath = "$/Sale", IterationPath = "$/7.1/Stake/Backlog" }));
                WorkItem.Add(us4);
                WorkItem.Add(us5);
            }
        }
#endregion

        public List<StateAndReason> GetNextStatesFor(string WIT, string curState)
        {
            var result = new List<StateAndReason>();
            result.Add(new StateAndReason("Active", "Activated"));
            result.Add(new StateAndReason("Inactive", "Passivated"));
            result.Add(new StateAndReason("Closed", "Completed"));
            return result;
        }

        public string WITStory { get { return "User Story"; } }
        public string WITTask { get { return "task"; } }
        public string WITBug { get { return "bug"; } }

        public string WITFPoints { get { return "Microsoft.VSTS.Scheduling.StoryPoints"; } }
        public string WITFHours { get { return "Microsoft.VSTS.Scheduling.OriginalEstimate"; } }
        public string WITFRemain { get { return "Microsoft.VSTS.Scheduling.RemainingWork"; } }


        public int PointsPrecision { get { return 0; } }
        public int HoursPrecision { get { return 1; } }
        public int RemainPrecision { get { return 1; } }
    }


}
